import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class LoginRegister {
    // 登录注册界面
    public static void showLoginRegister() {
        JFrame frame = Main.getMainFrame();
        frame.setTitle("迷宫闯关游戏");

        // 创建主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(50, 0, 50, 0); // 按钮上下间距
        gbc.anchor = GridBagConstraints.CENTER; // 组件在单元格内居中
        gbc.fill = GridBagConstraints.NONE;      // 不拉伸按钮
        gbc.weightx = 1.0;                       // 水平剩余空间分配
        gbc.weighty = 1.0;                       // 垂直剩余空间分配


        // 2. 创建登录按钮
        JButton loginButton = new JButton("登录");
        loginButton.setFont(new Font("SimHei", Font.PLAIN, 20));
        loginButton.setPreferredSize(new Dimension(200, 60)); // 按钮大小
        loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showLogin();
            }
        });


        // 3. 创建注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setFont(new Font("SimHei", Font.PLAIN, 20));
        registerButton.setPreferredSize(new Dimension(200, 60)); // 按钮大小
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showRegister();
            }
        });

        // 4. 添加按钮到主面板
        // 登录按钮放在第一行
        gbc.gridy = 0;
        gbc.insets = new Insets(170, 0, 0, 0); // 上、左、下、右间距
        mainPanel.add(loginButton, gbc);
        // 注册按钮放在第二行
        gbc.gridy = 1;
        gbc.insets = new Insets(0, 0, 170, 0); // 上、左、下、右间距
        mainPanel.add(registerButton, gbc);

        // 清空并设置新的内容面板
        frame.getContentPane().removeAll();
        frame.getContentPane().add(mainPanel);
        frame.revalidate();
        frame.repaint();
    }

    // 登录界面
    private static void showLogin() {
        JFrame frame = Main.getMainFrame();
        frame.setTitle("迷宫闯关游戏 - 登录");

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(50, 20, 50, 20));
        mainPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // 添加标题
        JLabel title = new JLabel("用户登录");
        title.setFont(new Font("SimHei", Font.BOLD, 30));
        title.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 50)));
        mainPanel.add(title);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加用户名行
        JPanel usernameRow = createInputRow("用户昵称:", 200);
        usernameRow.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(usernameRow);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 添加密码行
        JPanel passwordRow = createPasswordRow("用户密码:", 200);
        passwordRow.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(passwordRow);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 10)));

        // 添加确认登录按钮
        JButton loginButton = new JButton("确认登录");
        loginButton.setFont(new Font("SimHei", Font.PLAIN, 16));
        loginButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 登录成功，进入游戏
                GamePane.showGame();
            }
        });
        mainPanel.add(loginButton);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加返回按钮
        JButton backButton = new JButton("返回");
        backButton.setFont(new Font("SimHei", Font.PLAIN, 16));
        backButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showLoginRegister();
            }
        });
        mainPanel.add(backButton);
        mainPanel.add(Box.createVerticalGlue());

        // 清空并设置新的内容面板
        frame.getContentPane().removeAll();
        frame.getContentPane().add(mainPanel);
        frame.revalidate();
        frame.repaint();
    }

    // 注册界面
    private static void showRegister() {
        JFrame frame = Main.getMainFrame();
        frame.setTitle("迷宫闯关游戏 - 注册");

        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        mainPanel.setAlignmentX(Component.CENTER_ALIGNMENT);

        // 添加标题
        JLabel title = new JLabel("注册");
        title.setFont(new Font("SimHei", Font.BOLD, 30));
        title.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 50)));
        mainPanel.add(title);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加用户名行
        JPanel usernameRow = createInputRow("用户昵称:", 200);
        usernameRow.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(usernameRow);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));

        // 添加密码行
        JPanel passwordRow = createPasswordRow("用户密码:", 200);
        passwordRow.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(passwordRow);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));

        // 添加确认密码行
        JPanel confirmRow = createPasswordRow("确认密码:", 200);
        confirmRow.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(confirmRow);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 5)));

        // 添加确定按钮
        JButton confirmButton = new JButton("确定");
        confirmButton.setFont(new Font("SimHei", Font.PLAIN, 16));
        confirmButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 注册成功，返回登录界面
                showLoginRegister();
            }
        });
        mainPanel.add(confirmButton);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));

        // 添加返回按钮
        JButton backButton = new JButton("返回");
        backButton.setFont(new Font("SimHei", Font.PLAIN, 16));
        backButton.setAlignmentX(Component.CENTER_ALIGNMENT);
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Menu.showMainMenu();
            }
        });
        mainPanel.add(backButton);
        mainPanel.add(Box.createVerticalGlue());

        // 清空并设置新的内容面板
        frame.getContentPane().removeAll();
        frame.getContentPane().add(mainPanel);
        frame.revalidate();
        frame.repaint();
    }

    // 昵称输入行
    private static JPanel createInputRow(String labelText, double fieldWidth) {
        JPanel row = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 5));

        JLabel label = new JLabel(labelText);
        label.setFont(new Font("SimHei", Font.PLAIN, 16));
        label.setPreferredSize(new Dimension(80, 30));

        JTextField field = new JTextField();
        field.setPreferredSize(new Dimension((int) fieldWidth, 30));

        row.add(label);
        row.add(field);
        return row;
    }

    // 密码输入行
    private static JPanel createPasswordRow(String labelText, double fieldWidth) {
        JPanel row = new JPanel(new FlowLayout(FlowLayout.CENTER, 2, 5));

        JLabel label = new JLabel(labelText);
        label.setFont(new Font("SimHei", Font.PLAIN, 16));
        label.setPreferredSize(new Dimension(80, 30));

        JPasswordField field = new JPasswordField();
        field.setPreferredSize(new Dimension((int) fieldWidth, 30));

        row.add(label);
        row.add(field);
        return row;
    }
}